/* GSApepClient.java
 *
 * Project/Product: GWBS2009(数据发送)
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   Administrator	2009-3-18	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2009
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.hitrust.gs.integration.socket;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;

import com.apep.connect.APEPMessageEncoder;
import com.apep.connect.APEPRequest;
import com.apep.connect.APEPResponse;
import com.apep.connect.APEPServiceException;
import com.apep.connect.client.APEPClient;
import com.apep.lib.xml.SampleDOMParser;
import com.apep.lib.xml.SampleElement;
import com.hitrust.gs.business.GSCommunicationApepProcessor;
import com.hitrust.gs.lib.GSLogWriter;

public class GSGwbsClient extends APEPClient {
    public static String Remote="GWBS";
    private HashMap tRemoteInfo=new HashMap();
    public GSGwbsClient(){
        try {
            this.initialRemotetInfoConfigFromXml();
        } catch (Exception e) {
        }
    }
    
    private void initialRemotetInfoConfigFromXml() throws Exception {
        FileInputStream tFileInputStream = null;
        try {
            //1、取得配置文件中的远端信息
            String tClientInfoXmlPath = sSystemConfigurator.getParam("apep.RemotetInfo.xml");
            
            tFileInputStream = new FileInputStream(tClientInfoXmlPath);

            byte bBuf[] = new byte[102400];
            tFileInputStream.read(bBuf);

            
            //2、解析远程连接信息配置文件
            SampleDOMParser tPar = new SampleDOMParser();
            SampleElement tRootElement = tPar.parse(bBuf,"UTF-8");
            ArrayList tList = tRootElement.getElement("RemoteInfoConfig.Client");
           for(int i = 0; i<tList.size();i++){
                SampleElement tTmpElement = (SampleElement)tList.get(i);
                String tRemoteId   = tTmpElement.getChildElement("RemoteId").getTagValue();
                String tPassword   = tTmpElement.getChildElement("Password"  ).getTagValue();
                String tServicePassword="";
                if(tTmpElement.getChildElement("ServicePassword")!=null){
                    tServicePassword= tTmpElement.getChildElement("ServicePassword"  ).getTagValue();
                }
                String tDesKey     = tTmpElement.getChildElement("DesKey"    ).getTagValue();
                String tServiceUrl = tTmpElement.getChildElement("ServiceUrl").getTagValue();
                String tVersion    = tTmpElement.getChildElement("Version"   ).getTagValue();
                String tCharset    = tTmpElement.getChildElement("Charset"   ).getTagValue();
                APEPMessageEncoder tAPEPMessageEncoder = APEPMessageEncoder.getInstance(Integer.parseInt(sSystemConfigurator.getParam("apep.connect.max.msg.len")), tDesKey); 
                
                tRemoteInfo.put(tRemoteId + "|PWD"    , tPassword  );
                tRemoteInfo.put(tRemoteId + "|SERPWD"    , tServicePassword  );
                tRemoteInfo.put(tRemoteId + "|DESKEY" , tDesKey    );
                tRemoteInfo.put(tRemoteId + "|URL"    , tServiceUrl);
                tRemoteInfo.put(tRemoteId + "|VER"    , tVersion   );
                tRemoteInfo.put(tRemoteId + "|CHARSET", tCharset   );
                tRemoteInfo.put(tRemoteId + "|ENCODER", tAPEPMessageEncoder);

            }
        }
        catch (Exception e) {
            throw e;
        }
        finally {
            if(tFileInputStream != null)
                tFileInputStream.close();
        }
    }
    /**
     * 发送到Gwbs
     * **/
    public  APEPResponse sendToGwbs(APEPRequest tAPEPRequest) throws APEPServiceException{
        tAPEPRequest.setRequestId(tAPEPRequest.getAction()+System.currentTimeMillis());
        tAPEPRequest.setVersion(tRemoteInfo.get(Remote+"|VER").toString());
        tAPEPRequest.setReceiverId(Remote);
        tAPEPRequest.setPassword(tRemoteInfo.get(Remote+"|PWD").toString());
        tAPEPRequest.setCustomerId(GSCommunicationApepProcessor.CUSID);
        tAPEPRequest.setSenderId(GSCommunicationApepProcessor.CUSID);
        GSLogWriter.info("发送报文:"+tAPEPRequest.toXml());
        APEPResponse res= submit(tAPEPRequest);
        return res;
    }
}
